package com.testmanager.testmananger.quantitative.service;

import com.google.common.collect.Lists;
import com.testmanager.testmananger.foundation.util.FlakeIdGenerator;
import com.testmanager.testmananger.quantitative.constants.BugSearchStatus;
import com.testmanager.testmananger.quantitative.constants.Sign;
import com.testmanager.testmananger.quantitative.domain.BugInfoEntity;
import com.testmanager.testmananger.quantitative.domain.WorkerEntity;
import com.testmanager.testmananger.quantitative.dto.BugInfoDto;
import com.testmanager.testmananger.quantitative.dto.BugSearchDto;
import com.testmanager.testmananger.quantitative.dto.WorkerDto;
import com.testmanager.testmananger.quantitative.mapper.BugInfoMapper;
import com.testmanager.testmananger.quantitative.mapper.WorkerMapper;
import com.testmanager.testmananger.quantitative.repository.BugInfoRepository;
import com.testmanager.testmananger.quantitative.repository.WorkerRepository;
import com.testmanager.testmananger.user.repository.UserRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@Slf4j
public class BugInfoService {

    @Autowired
    BugInfoRepository bugInfoRepository;
    @Autowired
    WorkerRepository workerRepository;
    @Autowired
    WorkService workService;
    @Autowired
    FlakeIdGenerator flakeIdGenerator ;
    BugInfoMapper bugInfoMapper = new BugInfoMapper();
    WorkerMapper workerMapper = new WorkerMapper();
    public List<BugInfoDto> getBugsByItertionId(long iterationId){
        List<BugInfoEntity> bugInfoEntities = bugInfoRepository.findBugInfoEntitiesByIterationIdOrderByIdDesc(iterationId);
       return bugInfoEntities.stream()
                .map(t->{//获取worker
                    List<WorkerEntity> workerEntities=workerRepository.findWorkerEntitiesByOwnerNoOrderByIdDesc(t.getBugNo());
                    WorkerEntity  submitter = workerRepository.findWorkerEntityByOwnerNo(getSubmitNo(t.getBugNo()));
                    //WorkerMapper::toWorkerDto;
                    if(null == submitter){
                        submitter = new WorkerEntity();
                    }
                    if (workerEntities.size() == 0){
                        return bugInfoMapper.toBugInfoDto(t, workerMapper.toWorkerDto(new WorkerEntity()),workerMapper.toWorkerDto(submitter));
                    }
                    WorkerDto workerDto = workerMapper.toWorkerDto(workerEntities.get(0));

                   return bugInfoMapper.toBugInfoDto(t, workerDto,workerMapper.toWorkerDto(submitter));
                }).collect(Collectors.toList());
    }
    public List<BugInfoDto> searchBugs(BugSearchDto bugSearchDto) {
        List<BugInfoDto> bugInfoDtos = getBugsByItertionId(bugSearchDto.getOwnNo());
        List<BugInfoDto> filterResult = bugInfoDtos.stream()
                .filter(t -> {
                    if (BugSearchStatus.All.getValue() == bugSearchDto.getBugStatus()) return true;
                    return bugSearchDto.getBugStatus() == bugSearchDto.getBugStatus();
                })
                .filter(t-> {
                    if ("".equals(bugSearchDto.getRepairerName().trim())) return true;
                    if (null == t.getRepairer().getUserName()) return false;
                    return t.getRepairer().getUserName().equals(bugSearchDto.getRepairerName());
                })
                .filter(t->{
                    if ("".equals(bugSearchDto.getSubmiterName().trim())) return true;
                    if (null == t.getSubmiteer().getUserName()) return false;
                    return t.getSubmiteer().getUserName().equals(bugSearchDto.getSubmiterName());
                })
                .filter(t->{
                    if ("".equals(bugSearchDto.getContent().trim())) return true;
                    return  t.getDescribe().contains(bugSearchDto.getContent());
                })

                .collect(Collectors.toList());
       return  filterResult;
    }

    @Transactional
    public List<BugInfoDto> saveBugsByItertionId(BugInfoDto bugInfoDto,Long iterationId){
        log.info("保存更新bug信息:{},iterationID:{}",bugInfoDto,iterationId);
       // bugInfoDto.getBugNo() !=0?bugInfoDto.getBugNo():;
        if(bugInfoDto.getBugNo()==0){
            bugInfoDto.setBugNo(flakeIdGenerator.newId());
        }
        BugInfoEntity srcEntity;
        Optional<BugInfoEntity> byId = bugInfoRepository.findById(bugInfoDto.getId());
        if(!byId.isPresent()){//值不存在
           // return Lists.newArrayList();
            srcEntity = new BugInfoEntity();
        }else {
            srcEntity = byId.get();
        }



        BugInfoEntity bugInfoEntity = bugInfoMapper.toBugInfoEntity(srcEntity,bugInfoDto, iterationId);
       WorkerDto workerDto=bugInfoDto.getRepairer() ;



       WorkerEntity workerEntity =workerMapper.toWorkerEntity(bugInfoDto.getBugNo(), workerDto);
       WorkerEntity submitter = workerMapper.toWorkerEntity(getSubmitNo(bugInfoDto.getBugNo()),bugInfoDto.getSubmiteer());
        bugInfoRepository.save(bugInfoEntity);
        workService.saveWorker(workerEntity);
        workService.saveWorker(submitter);
      //  workerRepository.save(workerEntity);
       return getBugsByItertionId(iterationId);
    }


    private long getSubmitNo(long bugNo){
        return bugNo*10 +Sign.BugSubmitterSign;
    }
}
